﻿using DSPL.Contracts;
using DSPL.Contracts.Common;
using DSPL.Contracts.Interfaces;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DSPL.Common
{
    /// <summary>
    /// Common Textbox tool for handeling Location Details
    /// </summary>
    public partial class TxtLocation : UserControl
    {
        public TxtLocation()
        {
            InitializeComponent();
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            ResolveDependancies();
        }

        #region Declaring Properties

        #region General Properties

        IMessageService MessageService;
        ICommonValidations CommonValidations;

        public HelpNames HelpName
        {
            get { return (HelpNames)GetValue(HelpNameProperty); }
            set { SetValue(HelpNameProperty, value); }
        }
        
        public object LocationKeyValue
        {
            get { return (object)GetValue(LocationKeyValueProperty); }
            set { SetValue(LocationKeyValueProperty, value); }
        }

        public string KeyFieldName
        {
            get { return (string)GetValue(KeyFieldNameProperty); }
            set { SetValue(KeyFieldNameProperty, value); }
        }

        public string KeyFieldType
        {
            get { return (string)GetValue(KeyFieldTypeProperty); }
            set { SetValue(KeyFieldTypeProperty, value); }
        }

        public DataRow SelectedDataRow
        {
            get { return (DataRow)GetValue(SelectedDataRowProperty); }
            set { SetValue(SelectedDataRowProperty, value); }
        }

        public Modes ParentFormMode
        {
            get { return (Modes)GetValue(ParentFormModeProperty); }
            set { SetValue(ParentFormModeProperty, value); }
        }

        public bool IsMandatory
        {
            get { return (bool)GetValue(IsMandatoryProperty); }
            set { SetValue(IsMandatoryProperty, value); }
        }

        public string FilterString
        {
            get { return (string)GetValue(FilterStringProperty); }
            set { SetValue(FilterStringProperty, value); }
        }

        #endregion

        #region txtCode Properties

        public Visibility CodeVisibility
        {
            get { return (Visibility)GetValue(CodeVisibilityProperty); }
            set { SetValue(CodeVisibilityProperty, value); }
        }

        public bool CodeEnable
        {
            get { return (bool)GetValue(CodeEnableProperty); }
            set { SetValue(CodeEnableProperty, value); }
        }

        public string LocationCode
        {
            get { return (string)GetValue(LocationCodeProperty); }
            set { SetValue(LocationCodeProperty, value); }
        }

        public int CodeColumnSpan
        {
            get { return (int)GetValue(CodeColumnSpanProperty); }
            set { SetValue(CodeColumnSpanProperty, value); }
        }

        public string LocationCodeFieldName
        {
            get { return (string)GetValue(LocationCodeFieldNameProperty); }
            set { SetValue(LocationCodeFieldNameProperty, value); }
        }
        #endregion

        #region txtDescription Properties

        public Visibility DescriptionVisibility
        {
            get { return (Visibility)GetValue(DescriptionVisibilityProperty); }
            set { SetValue(DescriptionVisibilityProperty, value); }
        }

        public bool DescriptionEnable
        {
            get { return (bool)GetValue(DescriptionEnableProperty); }
            set { SetValue(DescriptionEnableProperty, value); }
        }

        public string LocationDescription
        {
            get { return (string)GetValue(LocationDescriptionProperty); }
            set { SetValue(LocationDescriptionProperty, value); }
        }

        public int DescriptionColumnSpan
        {
            get { return (int)GetValue(DescriptionColumnSpanProperty); }
            set { SetValue(DescriptionColumnSpanProperty, value); }
        }

        public int DescriptionColumn
        {
            get { return (int)GetValue(DescriptionColumnProperty); }
            set { SetValue(DescriptionColumnProperty, value); }
        }

        public bool IsFocus
        {
            get { return (bool)GetValue(IsFocusProperty); }
            set { SetValue(IsFocusProperty, value); }
        }

        public string LocationrDescriptionFieldName
        {
            get { return (string)GetValue(LocationDescriptionFieldNameProperty); }
            set { SetValue(LocationDescriptionFieldNameProperty, value); }
        }

        #endregion

        #endregion

        #region Dependency Properties

        public static readonly DependencyProperty HelpNameProperty = DependencyProperty.Register("HelpName", typeof(HelpNames), typeof(TxtLocation), new PropertyMetadata(HelpNames.AllLocationsHelp));

        public static readonly DependencyProperty LocationKeyValueProperty = DependencyProperty.Register("LocationKeyValue", typeof(object), typeof(TxtLocation), new PropertyMetadata(null));

        public static readonly DependencyProperty KeyFieldNameProperty = DependencyProperty.Register("KeyFieldName", typeof(string), typeof(TxtLocation), new PropertyMetadata(""));

        public static readonly DependencyProperty KeyFieldTypeProperty = DependencyProperty.Register("KeyFieldType", typeof(string), typeof(TxtLocation), new PropertyMetadata(null));

        public static readonly DependencyProperty SelectedDataRowProperty = DependencyProperty.Register("SelectedDataRow", typeof(DataRow), typeof(TxtLocation), new PropertyMetadata(null));

        public static readonly DependencyProperty CodeVisibilityProperty = DependencyProperty.Register("CodeVisibility", typeof(Visibility), typeof(TxtLocation), new PropertyMetadata(Visibility.Visible));

        public static readonly DependencyProperty CodeEnableProperty = DependencyProperty.Register("CodeEnable", typeof(bool), typeof(TxtLocation), new PropertyMetadata(true));

        public static readonly DependencyProperty LocationCodeProperty = DependencyProperty.Register("LocationCode", typeof(string), typeof(TxtLocation), new PropertyMetadata(""));

        public static readonly DependencyProperty LocationCodeFieldNameProperty = DependencyProperty.Register("LocationCodeFieldName", typeof(string), typeof(TxtLocation), new PropertyMetadata("LocCode"));

        public static readonly DependencyProperty LocationDescriptionFieldNameProperty = DependencyProperty.Register("LocationDescriptionFieldName", typeof(string), typeof(TxtLocation), new PropertyMetadata("LocName"));
        
        public static readonly DependencyProperty DescriptionVisibilityProperty = DependencyProperty.Register("DescriptionVisibility", typeof(Visibility), typeof(TxtLocation), new PropertyMetadata(Visibility.Visible));

        public static readonly DependencyProperty DescriptionEnableProperty = DependencyProperty.Register("DescriptionEnable", typeof(bool), typeof(TxtLocation), new PropertyMetadata(false));

        public static readonly DependencyProperty LocationDescriptionProperty = DependencyProperty.Register("LocationDescription", typeof(string), typeof(TxtLocation), new PropertyMetadata(""));

        public static readonly DependencyProperty ParentFormModeProperty = DependencyProperty.Register("ParentFormMode", typeof(Modes), typeof(TxtLocation), new PropertyMetadata(Modes.Undo));

        public static readonly DependencyProperty IsMandatoryProperty = DependencyProperty.Register("IsMandatory", typeof(bool), typeof(TxtLocation), new PropertyMetadata(false));

        public static readonly DependencyProperty CodeColumnSpanProperty = DependencyProperty.Register("CodeColumnSpan", typeof(int), typeof(TxtLocation), new PropertyMetadata(1));

        public static readonly DependencyProperty DescriptionColumnSpanProperty = DependencyProperty.Register("DescriptionColumnSpan", typeof(int), typeof(TxtLocation), new PropertyMetadata(1));

        public static readonly DependencyProperty DescriptionColumnProperty = DependencyProperty.Register("DescriptionColumn", typeof(int), typeof(TxtLocation), new PropertyMetadata(1));

        public static readonly DependencyProperty IsFocusProperty = DependencyProperty.Register("IsFocus", typeof(bool), typeof(TxtLocation), new PropertyMetadata(false));

        public static readonly DependencyProperty FilterStringProperty = DependencyProperty.Register("FilterString", typeof(string), typeof(TxtLocation), new PropertyMetadata(""));

        #endregion


        #region Exposable Event Handlers

        public event EventHandler<EventArgs> RefreshLocationRelatedData;             //use to refresh data regarding Location Master Text box

        #endregion

        #region Internal Events

        private void RefreshAttachedObjects(object sender, EventArgs e)
        {
            if (CommonValidations!=null && SelectedDataRow!=null)
            {
                if (!CommonValidations.UserWiseLocationValidation(LocationKeyValue))
                {
                    MessageService.ShowMessage(MessageText.UserwiseInvalidLocation, MessageType.WarningOK);
                    LocationKeyValue = null;
                    LocationCode = "";
                }
            }

            if (RefreshLocationRelatedData != null)
                RefreshLocationRelatedData(sender, e);
            
        }

        private void ResolveDependancies()
        {
            IUnityContainer container = GlobalVariables.GlobalUnityContainer;
            if (container != null)
            {
                MessageService = container.Resolve<IMessageService>();
                CommonValidations = container.Resolve<ICommonValidations>();
            }
        }

        #endregion
    }

}